package zdream.control.region;

import zdream.utils.math.Rectangle;

import static zdream.control.region.TerrainConstant.*;

/**
 * 地形的通用方法类
 * 
 * @author Zdream
 * @date 2019-04-29
 */
public class Terrains {

	/**
	 * 地形的文字转成码
	 */
	public static byte terrainCode(String s) {
		switch (s) {
		case TERRAIN_STRING_SOLID:
			return TERRAIN_SOLID;

		case TERRAIN_STRING_STAB_BALL:
			return TERRAIN_STAB_BALL;
		case TERRAIN_STRING_STAB_UP:
			return TERRAIN_STAB_UP;

		case TERRAIN_STRING_LADDER:
			return TERRAIN_LADDER;

		default:
			return TERRAIN_EMPTY;
		}
	}

	/**
	 * 地形的文字转成码
	 */
	public static byte waterTerrainCode(String s) {
		switch (s) {
		case TERRAIN_STRING_WATER: return TERRAIN_WATER;
		case TERRAIN_STRING_WATER_B_SF: return TERRAIN_WATER_BOTTOM_SURFACE;
		case TERRAIN_STRING_WATER_T_SF: return TERRAIN_WATER_TOP_SURFACE;
		}
		return TERRAIN_EMPTY;
	}

	public static boolean isWaterCode(String s) {
		return waterTerrainCode(s) != TERRAIN_EMPTY;
	}

	/**
	 * 地形是否为空, 指可以让角色穿过的地形, 但不包括刺;
	 * 允许的有: 空、楼梯
	 */
	public static boolean isEmpty(byte terrain) {
		return terrain == TERRAIN_EMPTY || terrain == TERRAIN_LADDER;
	}
	
	public static boolean isLadder(byte terrain) {
		return terrain == TERRAIN_LADDER;
	}

	public static boolean isStab(byte terrain) {
		return terrain == TERRAIN_STAB_BALL || terrain == TERRAIN_STAB_UP;
	}

	public static void getStabRange(byte terrain, int x, int y, Rectangle range) {
		switch (terrain) {
		case TERRAIN_STAB_BALL: {
			float margin = 51f / 256;
			float w = 1 - margin * 2;
			range.set(x + margin, y + margin, w, w);
		} break;
		case TERRAIN_STAB_UP: {
			float marginY = 76f / 256;
			float marginX = 12f / 256;
			float w = 1 - marginY;
			range.set(x + marginX, y, 1 - marginX * 2, w);
		} break;
		}
	}

	public static boolean hasWater(byte filler) {
		return (filler & FILLER_WATER) != 0;
	}

}
